﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using ConnectionObjects;
using TCPClient.RequestsToServerHandlers;

namespace TCPClient.UI
{
    public delegate void LoginSuccessfulHandler(object sender, EventArgs e);

    public partial class LoginForm : Form
    {
        private LoginHandler m_LoginHandler;
        private NetworkStream m_Stream;
        public event LoginSuccessfulHandler LoginSuccessful = null; 

        public LoginForm(NetworkStream stream)
        {
            InitializeComponent();
            labelErrorPassword.Visible = false;
            labelUserNameError.Visible = false;
            m_Stream = stream;
            textBoxPassword.PasswordChar = '*';
        }

        private void onLoginSuccessful(object typeOfUIToShow)
        {
            if(LoginSuccessful != null)
            {
                LoginSuccessful.Invoke(this, new LoginEventArgs() { LoginType = typeOfUIToShow});
            }
            this.Close();
        }

        private void buttonLogin_Click(object sender, EventArgs e)
        {
            if (checkThatValuesEntered())
            {
                login();
            }
        }

        private void login()
        {
            m_LoginHandler = new LoginHandler(textBoxPassword.Text, textBoxUserName.Text, m_Stream);
            OutConnectionObject loginResult = m_LoginHandler.LoginToServer();
            if (loginResult.IsErrorOccured())
            {
                MessageBox.Show(loginResult.ExceptionMessage, "Login Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
                textBoxPassword.Text = "";
                textBoxUserName.Text = "";
            }
            else
            {
                this.Visible = false;
                onLoginSuccessful(loginResult.RetVal);
            }
        }

        private bool checkThatValuesEntered()
        {
            bool isValuesEntered = true;
            if (string.IsNullOrEmpty(textBoxUserName.Text))
            {
                labelUserNameError.Visible = true;
                isValuesEntered = false;
            }
            if (string.IsNullOrEmpty(textBoxPassword.Text))
            {
                labelErrorPassword.Visible = true;
                isValuesEntered = false;
            }
            return isValuesEntered;
        }

        private void textBoxUserName_TextChanged(object sender, EventArgs e)
        {
            labelUserNameError.Visible = false;

        }

        private void textBoxPassword_TextChanged(object sender, EventArgs e)
        {
            labelErrorPassword.Visible = false;
        }
    }

    public class LoginEventArgs: EventArgs
    {
        public object LoginType { get; set; }
    }
}
